<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="组名" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入组名" />
                </el-form-item>
                <!-- <el-form-item label="关联用户" prop="userIds">
                    <el-input v-model="formData.userIds" placeholder="请输入关联用户" />
                </el-form-item> -->
				<!-- <el-form-item label="APP用户" prop="userIds">
				    <el-select
				v-model="formData.userIds"
						  multiple
						  placeholder="请选择站点负责人"
						  style="width: 240px"
						  >
							<el-option
				          v-for="site in dictData.sites"
				          :key="site.id"
				          :label="site.nickname"
				          :value="site.id"
				      ></el-option>
				    </el-select>
				  </el-form-item> -->
				<el-form-item label="权限" prop="authoritys">
				  <el-select v-model="formData.authoritys" multiple class="flex-1" clearable placeholder="请选择权限">
				    <el-option v-for="permission in permissionsOptions" :key="permission.value" :label="permission.label" :value="permission.value"/>
				  </el-select>
				</el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  groupEdit, groupAdd, groupDetail } from '@/api/usergroup'
import {siteManager} from '@/api/site'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑用户组' : '新增用户组'
})

const authorityMap = {
  '1': '查看',
  '2': '控制',
  '3': '参数',
  '4': '上报'
};
const permissionsOptions = [
  {label: '查看', value: '1'},
  {label: '控制', value: '2'},
  {label: '参数', value: '3'},
  {label: '上报', value: '4'}
];
const formData = reactive({
    id: '',
    name: '',
    authoritys: '',
    userIds: [],
})

// //存放站点数据
const dictData = reactive({
  sites: [],  // 存储站点名称数据
})
// //设置站点数据
onMounted(async () => {
  dictData.sites = await siteManager(); // 调用后端接口获取站点数据，并赋值给dictData.sites
  console.log(dictData.sites)
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键',
            trigger: ['blur']
        }
    ],
    name: [
        {
            required: true,
            message: '请输入组名',
            trigger: ['blur']
        }
    ],
    /* userIds: [
        {
            required: true,
            message: '请输入关联用户',
            trigger: ['blur']
        }
    ], */
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await groupEdit(data) : await groupAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
	//formData.userIds = formData.userIds && formData.userIds.split(',');
	formData.authoritys = formData.authoritys && formData.authoritys.split(',');
	console.log(formData.userIds)
}

const getDetail = async (row: Record<string, any>) => {
    const data = await groupDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
